package com.gdut.simple.presenter;

import com.gdut.simple.basic.BasicApplication;
import com.gdut.simple.common.RequestCallback;
import com.gdut.simple.common.ToastUtils;
import com.gdut.simple.contract.PostContract;
import com.gdut.simple.data.entity.Post;
import com.gdut.simple.model.IPostModel;
import com.gdut.simple.model.impl.PostModelImpl;

import java.util.List;

public class PostPresenter implements PostContract.Presenter {

    private PostContract.View mPostView;
    private IPostModel mPostModel;

    public PostPresenter(PostContract.View view) {
        this.mPostView = view;
        mPostModel = new PostModelImpl();
        mPostView.setPresenter(this);
    }

    @Override
    public void refresh() {
        if (mPostView.getUser() == null) {
            return;
        }
        mPostView.showRefreshProgress();
        mPostModel.refresh(mPostView.getUser(), 10, new RequestCallback<List<Post>>() {
            @Override
            public void onSuccess(List<Post> posts) {
                mPostView.hideRefreshProgress();
                mPostView.getItemList().clear();
                mPostView.getItemList().addAll(posts);
                mPostView.refreshList();
                mPostView.reset();
            }

            @Override
            public void onFailure(String message) {
                mPostView.hideRefreshProgress();
                mPostView.showMessage(message);
            }
        });
    }

    @Override
    public void loadMore(int page) {
        mPostModel.loadMore(mPostView.getUser(), page, 10, new RequestCallback<List<Post>>() {
            @Override
            public void onSuccess(List<Post> posts) {
                mPostView.getItemList().addAll(posts);
                mPostView.refreshList();
            }

            @Override
            public void onFailure(String message) {
                mPostView.showMessage(message);
            }
        });
    }
}
